﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Web.Mvc;
using System.Text;
using System.EnterpriseServices;
using Webdiyer.WebControls.Mvc;
using YiYiTong.Rocket.Business;
using YiYiTong.Rocket.Entity;
using YiYiTong.Rocket.Model.Manager;
using YiYiTong.Rocket.Helper;
using YiYiTong.Rocket.Model.ParamModel;
using YiYiTong.Rocket.EFDBFirst;
using System.Web;
using System.IO;
using System.Text.RegularExpressions;
using YiYiTong.Rocket.Model;

namespace YiYiTong.Rocket.ManageWeb.Controllers
{
    public class JiesuanController : BaseController
    {
        [Description("供应商待结算列表")]
        [ViewPage]
        public ActionResult WaitList(WaitJiesuanQueryModel queryModel)
        {
            queryModel.Trim();
            ViewBag.Title = "供应商待结算列表";
            ViewBag.paramModel = queryModel;
            var pagedlist = getWaitJiesuanModel(queryModel);
            return View(pagedlist);
        }

        [Description("供应商结算申请列表")]
        [ViewPage]
        public ActionResult ApplyList(WaitJiesuanQueryModel queryModel)
        {
            if (queryModel.jiesuan_status == 0)
                queryModel.jiesuan_status = (int)jiesuan_status.Applied;
            queryModel.Trim();
            ViewBag.Title = "供应商结算申请列表";
            ViewBag.paramModel = queryModel;
            var pagedlist = getWaitJiesuanModel(queryModel);
            return View(pagedlist);
        }

        [Description("结算单列表")]
        [ViewPage]
        public ActionResult JiesuanList(JiesuanQueryModel queryModel)
        {
            queryModel.Trim();
            ViewBag.Title = "结算单列表";
            ViewBag.paramModel = queryModel;
            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();
            var queryable = dbcontext.ADZ_JiesuanMaster.Where(t => t.is_del == 0);

            long supplier_id = 0;
            if (!string.IsNullOrWhiteSpace(queryModel.supplier_name))
            {
                var supinfo = dbcontext.ADZ_SupplierMaster.FirstOrDefault(t => t.supplier_name == queryModel.supplier_name);
                if (supinfo != null)
                {
                    supplier_id = supinfo.supplier_id;
                }
                queryable = queryable.Where(t => t.supplier_id == supplier_id);
            }
            if (queryModel.jiesuan_master_status != -10)
            {
                queryable = queryable.Where(t => t.jiesuan_status == queryModel.jiesuan_master_status);
            }


            int rowcounts = queryable.Count();
            ViewData["AllCount"] = rowcounts;

            var querylist = queryable.OrderByDescending(t => t.jiesuan_id)
                .Skip(queryModel.pageSize * (queryModel.pageIndex - 1))
                .Take(queryModel.pageSize)
                .ToList();

            var resultList = new List<JiesuanMasterModel>();
            querylist.ForEach(t =>
            {
                var mastermodel = new JiesuanMasterModel
                {
                    aDZ_JiesuanMaster = t
                };
                mastermodel.jiesuanOrderDetails = dbcontext.ADZ_JiesuanOrderDetails.Where(x => x.jiesuan_id == t.jiesuan_id).ToList();

                resultList.Add(mastermodel);
            });

            PagedList<JiesuanMasterModel> pagedlist = null;
            if (querylist != null && querylist.Count != 0)
                pagedlist = new PagedList<JiesuanMasterModel>(resultList, queryModel.pageIndex, queryModel.pageSize, rowcounts);
            return View(pagedlist);
        }
        [Description("财务审核结算")]
        [ViewPage]
        public ActionResult caiwujiesuanauding(long jiesuan_id)
        {
            ViewBag.Title = "财务审核结算";
            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();
            JiesuanMasterModel model = new JiesuanMasterModel();
            model.aDZ_JiesuanMaster = dbcontext.ADZ_JiesuanMaster.FirstOrDefault(t => t.jiesuan_id == jiesuan_id);
            model.jiesuanOrderDetails = dbcontext.ADZ_JiesuanOrderDetails.Where(t => t.jiesuan_id == jiesuan_id).ToList();
            return View(model);
        }
        [Description("总经理审核结算")]
        [ViewPage]
        public ActionResult managerjiesuanauding(long jiesuan_id)
        {
            ViewBag.Title = "总经理审核结算";
            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();
            JiesuanMasterModel model = new JiesuanMasterModel();
            model.aDZ_JiesuanMaster = dbcontext.ADZ_JiesuanMaster.FirstOrDefault(t => t.jiesuan_id == jiesuan_id);
            model.jiesuanOrderDetails = dbcontext.ADZ_JiesuanOrderDetails.Where(t => t.jiesuan_id == jiesuan_id).ToList();
            return View(model);
        }
        [Description("出纳付款")]
        [ViewPage]
        public ActionResult chunapay(long jiesuan_id)
        {
            ViewBag.Title = "出纳付款";
            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();
            JiesuanMasterModel model = new JiesuanMasterModel();
            model.aDZ_JiesuanMaster = dbcontext.ADZ_JiesuanMaster.FirstOrDefault(t => t.jiesuan_id == jiesuan_id);
            model.jiesuanOrderDetails = dbcontext.ADZ_JiesuanOrderDetails.Where(t => t.jiesuan_id == jiesuan_id).ToList();
            return View(model);
        }
        [Description("结算单详情")]
        [ViewPage]
        public ActionResult detailinfo(long jiesuan_id)
        {
            ViewBag.Title = "结算单详情";
            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();
            JiesuanMasterModel model = new JiesuanMasterModel();
            model.aDZ_JiesuanMaster = dbcontext.ADZ_JiesuanMaster.FirstOrDefault(t => t.jiesuan_id == jiesuan_id);
            model.jiesuanOrderDetails = dbcontext.ADZ_JiesuanOrderDetails.Where(t => t.jiesuan_id == jiesuan_id).ToList();
            return View(model);
        }


        public PagedList<WaitJiesuanModel> getWaitJiesuanModel(WaitJiesuanQueryModel queryModel)
        {

            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();

            IQueryable<ADZ_JiesuanOrderDetails> queryable = dbcontext.ADZ_JiesuanOrderDetails;
            if (!string.IsNullOrWhiteSpace(queryModel.order_no))
            {
                queryable = queryable.Where(t => t.order_no.Contains(queryModel.order_no));
            }
            if (!string.IsNullOrWhiteSpace(queryModel.supplier_name))
            {
                var supplier = dbcontext.ADZ_SupplierMaster.FirstOrDefault(t => t.supplier_name == queryModel.supplier_name);
                long supplier_id = 0;
                if (supplier != null)
                {
                    supplier_id = supplier.supplier_id;
                }
                queryable = queryable.Where(t => t.supplier_id == supplier_id);
            }
            if (queryModel.jiesuan_status != -10)
            {
                queryable = queryable.Where(t => t.jiesuan_status == queryModel.jiesuan_status);
            }
            var total_list = (from t in queryable
                              group t by 1 into g
                              select new
                              {
                                  order_money = g.Sum(t => t.order_money),
                                  supply_money = g.Sum(t => t.supply_money)
                              }).ToList();

            ViewData["order_money"] = total_list.Any() ? total_list[0].order_money : 0;
            ViewData["supply_money"] = total_list.Any() ? total_list[0].supply_money : 0;

            int rowcounts = queryable.Count();
            ViewData["AllCount"] = rowcounts;

            var querylist = queryable.OrderBy(t => t.order_id)
                .Skip(queryModel.pageSize * (queryModel.pageIndex - 1))
                .Take(queryModel.pageSize)
                .ToList();

            var resultList = new List<WaitJiesuanModel>();
            if (querylist.Any())
            {
                querylist.ForEach(t =>
                {
                    WaitJiesuanModel model = new WaitJiesuanModel();
                    model.aDZ_JiesuanOrderDetails = t;
                    if (t.supplier_id > 0)
                    {
                        model.aDZ_SupplierMaster = dbcontext.ADZ_SupplierMaster.FirstOrDefault(x => x.supplier_id == t.supplier_id);
                    }
                    if (t.jiesuan_id > 0)
                    {
                        model.aDZ_JiesuanMaster = dbcontext.ADZ_JiesuanMaster.FirstOrDefault(x => x.jiesuan_id == t.jiesuan_id);
                    }
                    resultList.Add(model);
                });
            }
            PagedList<WaitJiesuanModel> pagedlist = null;
            if (querylist != null && querylist.Count != 0)
                pagedlist = new PagedList<WaitJiesuanModel>(resultList, queryModel.pageIndex, queryModel.pageSize, rowcounts);
            return pagedlist;
        }

        [Description("申请供应商结算")]
        [HttpPost]
        public ActionResult applyjiesuan(string ids)
        {
            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();
            ResponseMsg resMsg = new ResponseMsg();
            resMsg.IsSuccess = false;
            resMsg.Message = "操作失败";
            if (string.IsNullOrWhiteSpace(ids))
            {
                resMsg.IsSuccess = false;
                resMsg.Message = $"传入的信息不对";
                return Json(resMsg, JsonRequestBehavior.AllowGet);
            }
            if (ids.EndsWith(","))
                ids = ids.Substring(0, ids.Length - 1);
            string[] id_arr = Regex.Split(ids, ",");
            var aDZ_JiesuanOrderDetails = dbcontext.ADZ_JiesuanOrderDetails.Where(t => id_arr.Contains(t.seq_id.ToString())).ToList();
            foreach (var item in aDZ_JiesuanOrderDetails)
            {
                if (item.jiesuan_status > (int)jiesuan_status.Created)
                {
                    resMsg.Message = $"您选的订单{item.order_no}已经申请结算了，请勿重复申请！";
                    return Json(resMsg, JsonRequestBehavior.AllowGet);
                }
                item.jiesuan_status = (int)jiesuan_status.Applied;
                item.apply_time = DateTime.Now;
                item.apply_userid = new CurrentUser().user.uid;
                item.apply_username = new CurrentUser().user.realName;
            }


            dbcontext.SaveChanges();
            resMsg.IsSuccess = true;
            resMsg.Message = "操作成功";
            return Json(resMsg, JsonRequestBehavior.AllowGet);
        }

        [Description("创建供应商结算")]
        [ViewPage]
        public ActionResult Create(CreateJiesuanModel model)
        {
            ViewBag.Title = "创建供应商结算";
            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();
            if (model.cbx_last.Count > 0)
            {
                long seq_id = ConvertHelper.ConvertTo<long>(model.cbx_last[0], 0);
                var supperdetails = dbcontext.ADZ_JiesuanOrderDetails.FirstOrDefault(t => t.seq_id == seq_id);
                if (supperdetails != null)
                {
                    model.aDZ_JiesuanMaster.supplier_id = supperdetails.supplier_id;
                }
                var aDZ_JiesuanOrderDetails = dbcontext.ADZ_JiesuanOrderDetails.Where(t => model.cbx_last.Contains(t.seq_id.ToString())).ToList();
                decimal order_money = 0;
                decimal supply_money = 0;
                foreach (var item in aDZ_JiesuanOrderDetails)
                {
                    order_money += item.order_money;
                    supply_money += item.supply_money;
                }
                model.aDZ_JiesuanMaster.order_money = order_money;
                model.aDZ_JiesuanMaster.supply_money = supply_money;
            }
            ViewBag.paramModel = model;
            return View(model);
        }

        [Description("创建供应商结算")]
        [HttpPost]
        public ActionResult create(CreateJiesuanModel model)
        {
            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();
            ResponseMsg resMsg = new ResponseMsg();
            resMsg.IsSuccess = false;
            resMsg.Message = "操作失败";

            decimal order_money = 0;
            decimal supply_money = 0;
            var aDZ_JiesuanOrderDetails = dbcontext.ADZ_JiesuanOrderDetails.Where(t => model.cbx_last.Contains(t.seq_id.ToString())).ToList();
            //检查cbx_last 是不是正确
            long suppler_id_tmp = 0;

            foreach (var item in aDZ_JiesuanOrderDetails)
            {
                if (item.jiesuan_status != (int)jiesuan_status.Applied)
                {
                    resMsg.Message = $"您选的订单【{item.order_no}】状态不对，请检查！";
                    return Json(resMsg, JsonRequestBehavior.AllowGet);
                }
                if (suppler_id_tmp > 0)
                {
                    if (suppler_id_tmp != item.supplier_id)
                    {
                        resMsg.Message = $"同一个结算订单里的供应商必须一致！";
                        return Json(resMsg, JsonRequestBehavior.AllowGet);
                    }
                }
                suppler_id_tmp = item.supplier_id;
                order_money += item.order_money;
                supply_money += item.supply_money;
            }

            model.aDZ_JiesuanMaster.order_money = order_money;
            model.aDZ_JiesuanMaster.supply_money = supply_money;
            model.aDZ_JiesuanMaster.create_time = DateTime.Now;
            model.aDZ_JiesuanMaster.last_time = DateTime.Now;
            var detail = dbcontext.ADZ_SupplierDetails.FirstOrDefault(t => t.seq_id == model.aDZ_JiesuanMaster.supplierdetails_id);
            if (detail != null)
            {
                model.aDZ_JiesuanMaster.channel = detail.channel.Value;
                model.aDZ_JiesuanMaster.channel_info = JsonHelper.Serialize(detail);
            }
            model.aDZ_JiesuanMaster.apply_userid = new CurrentUser().user.uid;
            model.aDZ_JiesuanMaster.apply_username = new CurrentUser().user.realName;
            model.aDZ_JiesuanMaster.order_info = JsonHelper.Serialize(aDZ_JiesuanOrderDetails);

            var supplier_master = dbcontext.ADZ_SupplierMaster.FirstOrDefault(t => t.supplier_id == model.aDZ_JiesuanMaster.supplier_id);
            if (supplier_master != null)
            {
                model.aDZ_JiesuanMaster.supplier_name = supplier_master.supplier_name;
            }
            LogHelper.Debug(JsonHelper.Serialize(model.aDZ_JiesuanMaster));
            dbcontext.ADZ_JiesuanMaster.Add(model.aDZ_JiesuanMaster);

            dbcontext.SaveChanges();

            if (aDZ_JiesuanOrderDetails.Any())
            {
                aDZ_JiesuanOrderDetails.ForEach(t =>
                {
                    t.jiesuan_id = model.aDZ_JiesuanMaster.jiesuan_id;
                    t.jiesuan_status = (byte)jiesuan_status.Auditing;
                });
            }

            dbcontext.SaveChanges();

            JiesuanBiz.Instance.WriteJiesuanLog(new ADZ_JiesuanLog
            {
                jiesuan_id = model.aDZ_JiesuanMaster.jiesuan_id,
                op_msg = "创建结算单",
                op_uid = new CurrentUser().user.uid,
                op_username = new CurrentUser().user.realName,
                remark = JsonHelper.Serialize(aDZ_JiesuanOrderDetails)
            });
            resMsg.IsSuccess = true;
            resMsg.Message = "操作成功";
            return Json(resMsg, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="jiesuan_id"></param>
        /// <returns></returns>
        [Description("财务驳回供应商结算")]
        [HttpPost]
        public ActionResult caiwurefusejiesuan(long jiesuan_id, string reject_reson)
        {
            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();
            ResponseMsg resMsg = new ResponseMsg();
            resMsg.IsSuccess = false;
            resMsg.Message = "操作失败";

            var aDZ_JiesuanOrderDetails = dbcontext.ADZ_JiesuanOrderDetails.Where(t => t.jiesuan_id == jiesuan_id).ToList();
            foreach (var item in aDZ_JiesuanOrderDetails)
            {
                if (item.jiesuan_status != (int)jiesuan_status.Auditing)
                {
                    resMsg.Message = $"您选的订单{item.order_no}状态不对，不能操作！";
                    return Json(resMsg, JsonRequestBehavior.AllowGet);
                }
                item.jiesuan_status = (int)jiesuan_status.Created;

            }

            var master = dbcontext.ADZ_JiesuanMaster.FirstOrDefault(t => t.jiesuan_id == jiesuan_id);
            if (master == null)
            {
                resMsg.Message = $"结算订单不正确！";
                return Json(resMsg, JsonRequestBehavior.AllowGet);
            }
            if (master.jiesuan_status != (int)jiesuan_master_status.Created)
            {
                resMsg.Message = $"结算单状态不对，不能驳回，请检查！";
                return Json(resMsg, JsonRequestBehavior.AllowGet);
            }

            master.jiesuan_status = (int)jiesuan_master_status.CaiwuRefuse;
            master.reject_reson = reject_reson;

            dbcontext.SaveChanges();
            JiesuanBiz.Instance.WriteJiesuanLog(new ADZ_JiesuanLog
            {
                jiesuan_id = jiesuan_id,
                op_msg = "财务驳回结算单",
                op_uid = new CurrentUser().user.uid,
                op_username = new CurrentUser().user.realName,
                remark = reject_reson
            });
            resMsg.IsSuccess = true;
            resMsg.Message = "操作成功";
            return Json(resMsg, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="jiesuan_id"></param>
        /// <returns></returns>
        [Description("财务提交总经理审核")]
        [HttpPost]
        public ActionResult caiwupassjiesuan(long jiesuan_id,decimal jiesuan_money_real, string remark)
        {
            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();
            ResponseMsg resMsg = new ResponseMsg();
            resMsg.IsSuccess = false;
            resMsg.Message = "操作失败";
            if(jiesuan_money_real <= 0)
            {
                resMsg.Message = $"实际结算金额不正确！";
                return Json(resMsg, JsonRequestBehavior.AllowGet);
            }

            var aDZ_JiesuanOrderDetails = dbcontext.ADZ_JiesuanOrderDetails.Where(t => t.jiesuan_id == jiesuan_id).ToList();
            foreach (var item in aDZ_JiesuanOrderDetails)
            {
                if (item.jiesuan_status != (int)jiesuan_status.Auditing)
                {
                    resMsg.Message = $"您选的订单{item.order_no}状态不对，不能操作！";
                    return Json(resMsg, JsonRequestBehavior.AllowGet);
                }
            }
            var master = dbcontext.ADZ_JiesuanMaster.FirstOrDefault(t => t.jiesuan_id == jiesuan_id);
            if (master == null)
            {
                resMsg.Message = $"结算订单不正确！";
                return Json(resMsg, JsonRequestBehavior.AllowGet);
            }
            if(master.jiesuan_status != (int)jiesuan_master_status.Created)
            {
                resMsg.Message = $"结算单状态不对，不能审核，请检查！";
                return Json(resMsg, JsonRequestBehavior.AllowGet);
            }
            master.jiesuan_money_real = jiesuan_money_real;
            master.jiesuan_status = (int)jiesuan_master_status.CaiwuPassed;
            master.last_time = DateTime.Now;
            dbcontext.SaveChanges();
            //记录结算日志
            JiesuanBiz.Instance.WriteJiesuanLog(new ADZ_JiesuanLog
            {
                jiesuan_id = jiesuan_id,
                op_msg = "财务审核通过结算单",
                op_uid = new CurrentUser().user.uid,
                op_username = new CurrentUser().user.realName,
                remark = remark
            });
            resMsg.IsSuccess = true;
            resMsg.Message = "操作成功";
            return Json(resMsg, JsonRequestBehavior.AllowGet);
        }












        /// <summary>
        /// 
        /// </summary>
        /// <param name="jiesuan_id"></param>
        /// <returns></returns>
        [Description("总经理驳回供应商结算")]
        [HttpPost]
        public ActionResult managerrefusejiesuan(long jiesuan_id, string reject_reson)
        {
            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();
            ResponseMsg resMsg = new ResponseMsg();
            resMsg.IsSuccess = false;
            resMsg.Message = "操作失败";

            var aDZ_JiesuanOrderDetails = dbcontext.ADZ_JiesuanOrderDetails.Where(t => t.jiesuan_id == jiesuan_id).ToList();
            foreach (var item in aDZ_JiesuanOrderDetails)
            {
                if (item.jiesuan_status != (int)jiesuan_status.Auditing)
                {
                    resMsg.Message = $"您选的订单{item.order_no}状态不对，不能操作！";
                    return Json(resMsg, JsonRequestBehavior.AllowGet);
                }
                item.jiesuan_status = (int)jiesuan_status.Created;

            }

            var master = dbcontext.ADZ_JiesuanMaster.FirstOrDefault(t => t.jiesuan_id == jiesuan_id);
            if (master == null)
            {
                resMsg.Message = $"结算订单不正确！";
                return Json(resMsg, JsonRequestBehavior.AllowGet);
            }
            if (master.jiesuan_status != (int)jiesuan_master_status.CaiwuPassed)
            {
                resMsg.Message = $"结算单状态不对，不能驳回，请检查！";
                return Json(resMsg, JsonRequestBehavior.AllowGet);
            }

            master.jiesuan_status = (int)jiesuan_master_status.ManagerRefuse;
            master.reject_reson = reject_reson;

            dbcontext.SaveChanges();
            JiesuanBiz.Instance.WriteJiesuanLog(new ADZ_JiesuanLog
            {
                jiesuan_id = jiesuan_id,
                op_msg = "总经理驳回结算单",
                op_uid = new CurrentUser().user.uid,
                op_username = new CurrentUser().user.realName,
                remark = reject_reson
            });
            resMsg.IsSuccess = true;
            resMsg.Message = "操作成功";
            return Json(resMsg, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="jiesuan_id"></param>
        /// <returns></returns>
        [Description("总经理审核通过")]
        [HttpPost]
        public ActionResult managerpassjiesuan(long jiesuan_id, string remark)
        {
            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();
            ResponseMsg resMsg = new ResponseMsg();
            resMsg.IsSuccess = false;
            resMsg.Message = "操作失败";

            var aDZ_JiesuanOrderDetails = dbcontext.ADZ_JiesuanOrderDetails.Where(t => t.jiesuan_id == jiesuan_id).ToList();
            foreach (var item in aDZ_JiesuanOrderDetails)
            {
                if (item.jiesuan_status != (int)jiesuan_status.Auditing)
                {
                    resMsg.Message = $"您选的订单{item.order_no}状态不对，不能操作！";
                    return Json(resMsg, JsonRequestBehavior.AllowGet);
                }
            }
            var master = dbcontext.ADZ_JiesuanMaster.FirstOrDefault(t => t.jiesuan_id == jiesuan_id);
            if (master == null)
            {
                resMsg.Message = $"结算订单不正确！";
                return Json(resMsg, JsonRequestBehavior.AllowGet);
            }
            if (master.jiesuan_status != (int)jiesuan_master_status.CaiwuPassed)
            {
                resMsg.Message = $"结算单状态不对，不能审核，请检查！";
                return Json(resMsg, JsonRequestBehavior.AllowGet);
            }
            master.jiesuan_status = (int)jiesuan_master_status.ManagerPass;
            master.last_time = DateTime.Now;
            dbcontext.SaveChanges();
            //记录结算日志
            JiesuanBiz.Instance.WriteJiesuanLog(new ADZ_JiesuanLog
            {
                jiesuan_id = jiesuan_id,
                op_msg = "总经理审核通过结算单",
                op_uid = new CurrentUser().user.uid,
                op_username = new CurrentUser().user.realName,
                remark = remark
            });
            resMsg.IsSuccess = true;
            resMsg.Message = "操作成功";
            return Json(resMsg, JsonRequestBehavior.AllowGet);
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="jiesuan_id"></param>
        /// <returns></returns>
        [Description("出纳付款")]
        [HttpPost]
        public ActionResult chunapay(ADZ_JiesuanMaster model)
        {
            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();
            ResponseMsg resMsg = new ResponseMsg();
            resMsg.IsSuccess = false;
            resMsg.Message = "操作失败";

            HttpFileCollection hfc = System.Web.HttpContext.Current.Request.Files;
            if (hfc != null && hfc.Count > 0)
            {
                var file = hfc[0];
                var putResult = ImageFileBiz.UpImg(file, "jiesuan");
                if (putResult.Item1)
                    model.pay_img = putResult.Item3;
            }

            if (string.IsNullOrWhiteSpace(model.pay_img))
            {
                resMsg.Message = $"请上传支付凭证图片！";
                return Json(resMsg, JsonRequestBehavior.AllowGet);
            }
            

            var aDZ_JiesuanOrderDetails = dbcontext.ADZ_JiesuanOrderDetails.Where(t => t.jiesuan_id == model.jiesuan_id).ToList();
            foreach (var item in aDZ_JiesuanOrderDetails)
            {
                if (item.jiesuan_status != (int)jiesuan_status.Auditing)
                {
                    resMsg.Message = $"您选的订单{item.order_no}状态不对，不能操作！";
                    return Json(resMsg, JsonRequestBehavior.AllowGet);
                }
                item.jiesuan_status = (int)jiesuan_status.Finished;
            }
            var master = dbcontext.ADZ_JiesuanMaster.FirstOrDefault(t => t.jiesuan_id == model.jiesuan_id);
            if (master == null)
            {
                resMsg.Message = $"结算订单不正确！";
                return Json(resMsg, JsonRequestBehavior.AllowGet);
            }
            if (master.jiesuan_status != (int)jiesuan_master_status.ManagerPass)
            {
                resMsg.Message = $"总经理还未审核通过，不能付款！";
                return Json(resMsg, JsonRequestBehavior.AllowGet);
            }
            master.jiesuan_status = (int)jiesuan_master_status.Finished;
            master.pay_img = model.pay_img;
            master.pay_time = DateTime.Now;
            dbcontext.SaveChanges();
            //记录结算日志
            JiesuanBiz.Instance.WriteJiesuanLog(new ADZ_JiesuanLog
            {
                jiesuan_id = model.jiesuan_id,
                op_msg = "出纳上传付款凭证，付款完成",
                op_uid = new CurrentUser().user.uid,
                op_username = new CurrentUser().user.realName
            });
            resMsg.IsSuccess = true;
            resMsg.Message = "操作成功";
            return Json(resMsg, JsonRequestBehavior.AllowGet);
        }
    }
}